package com.itheima.supermarket.manage.controller;

import com.itheima.supermarket.manage.domain.Fruits;
import com.itheima.supermarket.manage.servicer.FruitServicer;

import java.util.ArrayList;
import java.util.Scanner;

public class CustomerController {

    private Scanner sc = new Scanner(System.in);
    FruitServicer fs = new FruitServicer();
    //顾客购买水果原始总价
    double total;

    //顾客界面
    public void customerController() {

        while (true) {
            System.out.println("====================顾客您好！====================");
            System.out.println("今日活动：满100元立减20元！满200元立减50元！");
            System.out.println("请选择：1.查看水果      2.购买水果      3.付款      4.退出");
            String choice = sc.next();

            switch (choice) {
                case "1":
                    //System.out.println("查看水果");
                    checkFruits();
                    break;
                case "2":
                    //System.out.println("购买水果");
                    buyFruits();
                    break;
                case "3":
                    //System.out.println("付款");
                    payFruits();
                    break;
                case "4":
                    System.out.println("谢谢惠顾！");
                    return;
                case "5":
                    System.out.println("您的输入有误，请重新输入！");
                    break;
            }
        }
    }

    //付款
    private void payFruits() {
        if (total >= 200) {
            double num = total - 50;
            System.out.println("您购买的水果总价为：" + total + "元，优惠后价格为：" + num + "元！");
        } else if (total >= 100 && total < 200) {
            double num = total - 20;
            System.out.println("您购买的水果总价为：" + total + "元，优惠后价格为：" + num + "元！");
        } else if (total > 0 && total < 100) {
            System.out.println("您购买的水果总价为：" + total + "元！");
        } else if (total == 0) {
            System.out.println("请您选择购买后再进行付款！");
        }
        //一次购买后，总金额归零，避免多次付款错误
        total = 0;
    }

    //购买水果
    private void buyFruits() {

        //拿到水果集合
        ArrayList<Fruits> fruits = fs.getFruits();
        if (fruits != null) {
            int a = 1;
            //打印选择水果菜单
            System.out.println("请选择您要购买的水果：");
            for (Fruits fruit : fruits) {
                System.out.print(a + "." + fruit.getName() + "\t\t");
                if (a % 5 == 0) {
                    System.out.println();
                }
                a++;
            }

            //设置退出出口
            System.out.println(a + ".退出请按x");

            //水果选择逻辑
            for (Fruits fruit : fruits) {
                if (total == 0) {
                    System.out.print("请开始选择：");
                }
                String number = sc.next();
                //判断是否退出
                if ("x".equals(number) && (total == 0)) {
                    return;
                } else if ("x".equals(number) && (total > 0)) {
                    System.out.println("请您及时付款！");
                    return;
                }
                //判断输入选择是否符合标准
                int number1 = Integer.parseInt(number);
                if (!(number1 >= 1 && number1 <= a)) {
                    System.out.println("输入有误，请重新输入！");
                    break;
                }
                System.out.print(fruit.getName() + "\t\t" + "请输入购买数量（kg）：");
                //用字符串接收数据
                String str = sc.next();
                //将接收到的价格字符串转换为字符数组，用于判断
                char[] chars = str.toCharArray();
                //判断输入数据是否位数字
                for (int i = 0; i < chars.length; i++) {
                    if (!((chars[i] >= '0' && chars[i] <= '9') || (chars[i] == '.'))) {
                        System.out.println("您的输入有误，请重新输入！");
                        return;
                    }
                }
                double num = Double.parseDouble(str);
                total += num * Double.parseDouble(fruit.getPrice());
                System.out.print("请继续选择（退出请按x）：");
            }
        } else {
            System.out.println("暂无水果信息！");
        }
    }

    //查看水果
    public void checkFruits() {
        //拿到水果集合
        ArrayList<Fruits> fruits = fs.getFruits();

        //判断水果库存有无水果
        if (fruits != null) {
            System.out.println("水果编号" + "\t\t" + "名称" + "\t\t" + "单价" + "\t" + "单位");
            for (Fruits fruit : fruits) {
                System.out.println(fruit.getNumber() + "\t\t" +
                        fruit.getName() + "\t\t" +
                        fruit.getPrice() + "\t\t" +
                        fruit.getUnit());
            }
        } else {
            System.out.println("暂无水果信息！");
        }
    }
}
